<template>
    <div class="app-container">
        <div style="text-align: center;margin-bottom: 30px">
            警报设置{{ createStatus ? '新增' : '更新' }}
        </div>
        <el-form :model="actionForm" ref="actionForm" label-width="100px">
            <el-row :gutter="10">
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="行动名称" prop="name">
                        <el-input type="text" v-model="actionForm.name" placeholder="请输入行动名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="动作编码" prop="code">
                        <el-input v-model="actionForm.code" placeholder="请输入动作编码" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="响应时长" prop="respond">
                        <el-input v-model="actionForm.respond" placeholder="请输入响应时长（s）" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="执行用户" prop="executiveUser">
                        <el-select v-model="actionForm.executiveUser" placeholder="请选择执行用户" style="width: 100%">
                            <el-option :value="0" label="全员 "></el-option>
                            <el-option :value="1" label="巡查员 "></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="动作内容" prop="action">
                        <el-input v-model="actionForm.action" placeholder="请输入动作内容" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="超时动作内容" prop="overtimeAction">
                        <el-input v-model="actionForm.overtimeAction" placeholder="请输入超时动作内容" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" style="text-align: center" class="dialog-footer">
            <el-button @click="cancelAction">取 消</el-button>
            <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createAction">创 建
            </el-button>
            <el-button :loading="addLoading" v-else type="primary" @click="updateAction">修 改</el-button>
        </div>
    </div>
</template>

<script>
import {getInFireAlarmActionVo,addInFireAlarmAction,updateInFireAlarmAction} from "@/api/fireAlarmAction";


export default {
    name: "ActionEdit",
    data() {
        return {

            actionForm: {

            },
            addLoading: false,
        }
    },
    computed: {
        createStatus() {
            return this.$route.params.ucId == '0'
        },
    },
    created() {
        if (!this.createStatus) {
            let ucId = this.$route.params.ucId
            this.getInFireAlarmActionVo(ucId)
        }
    },
    methods: {
        getInFireAlarmActionVo(val) {
            getInFireAlarmActionVo({ucId: val}).then(res => {
                this.actionForm = res.data
            }).catch(res => {

            })
        },
        cancelAction() {
          window.history.back()
        },
        createAction() {
            this.addLoading = true
            this.$refs['actionForm'].validate(valid => {
                if (valid) {
                    addInFireAlarmAction(this.actionForm).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加报警配置成功"
                        })
                        this.cancelAction()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        updateAction() {
            this.addLoading = true
            this.$refs['actionForm'].validate(valid => {
                if (valid) {
                    updateInFireAlarmAction(this.actionForm).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新报警配置成功"
                        })
                        this.cancelAction()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },

    }

}
</script>
